package file;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.Random;

/**
 * @author guoqf
 * @date 2021/3/4
 */
public class FileTimeUtils {

    private static final Random RANDOM = new Random();

    public static void changeLastUpdatedTime(File changeFile) throws IOException {
        if (changeFile.isFile()) {
//change file
            changeFileLastUpdatedTime(changeFile);
        } else {
            changeDirectoryLastUpdatedTime(changeFile);

        }

    }


    public static void changeFileLastUpdatedTime(File changeFile) throws IOException {
        long rangeLong = startTimeInMillis + (((long) (RANDOM.nextDouble() * intervalInMillis)));
        Files.setLastModifiedTime(Paths.get(changeFile.toURI()), fromTimeMillis(rangeLong));
    }


    public static void changeDirectoryLastUpdatedTime(File changeDirectory) throws IOException {


        File[] files = changeDirectory.listFiles();
        if (files != null) {
            for (File file : files) {
                changeLastUpdatedTime(file);
            }
        }
        long rangeLong = startTimeInMillis + (((long) (RANDOM.nextDouble() * intervalInMillis)));
        Files.setLastModifiedTime(Paths.get(changeDirectory.toURI()), fromTimeMillis(rangeLong));
    }

//    private static ExecutorService executorService = Executors.newFixedThreadPool(20);

    private static Long startTimeInMillis;
    private static Long intervalInMillis;


    private static FileTime fromTimeMillis(long timeMillis) {
        Date date = new Date(timeMillis);
        LocalDateTime newLocalDate = date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
        // convert LocalDate to instant, need time zone
        Instant instant = newLocalDate.toInstant(ZoneOffset.ofHours(8));
        return FileTime.from(instant);
    }

    public static void main(String[] args) throws ParseException {
        if (args == null || args.length < 3) {
            System.out.println("参数不合法退出parameters is illegal,should contains 'filePath','startTime'(eg: 2021-03-04 09:30:00) and 'endTime'(eg: 2021-03-04 18:00:00), exit.");
            return;
        }
//        String filePath = "D:\\迅雷下载";
        String filePath = args[0];
        String startTime = args[1];
        String endTime = args[2];
        File file = new File(filePath);
        String formatter = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat startFormat = new SimpleDateFormat(formatter);
        startTimeInMillis = startFormat.parse(startTime).getTime();
        SimpleDateFormat endFormat = new SimpleDateFormat(formatter);
        Long endTimeInMillis = endFormat.parse(endTime).getTime();
        intervalInMillis = endTimeInMillis - startTimeInMillis;
        System.out.printf("开始更改时间start to change last update time, file=[%s], startTime=[%s], endTime=[%s]%n", filePath, startTime, endTime);
        boolean b = true;
        try {
            changeLastUpdatedTime(file);
        } catch (IOException e) {
             b = false;
            e.printStackTrace();
        }
        System.out.printf("更改时间结束Finish changing last update time: [%s] %n", (b ? "SUCCESS" : "FAILURE"));




    }




}
